const Router = require('@koa/router');
const Server = require('../utils/httpServer');
const resultObj = require('../utils/resultCreator');
const jokeRouter = new Router({
    prefix: '/joke'
});

jokeRouter.get('/:page', async function (ctx) {
    const page = ctx.params.page;
    const host = '3g.163.com';
    const path = `/touch/jsonp/joke/chanListNews/T1419316284722/2/${page}-20.html`;
    const data = {};
    // false:http请求  true:https请求

    try {
        const body = await Server.httpGet(host, data, path, true);
        const list = JSON.parse(body)['段子'];
        // const arr = []
        // for (const i in list) {
        //   arr.push({
        //     title: list[i].title,
        //     source: list[i].source,
        //     digest: list[i].digest
        //   })
        // }

        ctx.body = resultObj(200, '查询成功', list);
    } catch (error) {
        ctx.body = resultObj(404, '网络错误');
        console.log(err);
    }
});

jokeRouter.get('/:type/:page', async function (ctx) {
    let id;
    const type = parseInt(ctx.params.type);
    const page = parseInt(ctx.params.page);
    // 0搞笑视频  1美女视频  2体育视频  3 新闻现场 4涨姿势  5猎奇  6 黑科技 默认搞笑视频

    switch (type) {
    case 0:
        id = 'VAP4BFE3U';
        break;
    case 1:
        id = 'VAP4BG6DL';
        break;
    case 2:
        id = 'VBF8F2E94';
        break;
    case 3:
        id = 'VAV3H6JSN';
        break;
    case 4:
        id = 'VBF8F3SGL';
        break;
    case 5:
        id = 'VBF8ET3S2';
        break;
    case 6:
        id = 'VBF8F2PKF';
        break;
    default:
        id = 'VAP4BFE3U';
    }

    const host = 'c.m.163.com';
    const path = `/nc/video/list/${id}/y/${page}-20.html`;
    // false:http请求  true:https请求
    const body = await Server.httpGet(host, {}, path, true);
    const arr = JSON.parse(body);

    ctx.body = resultObj(200, '请求成功', arr[id]);
});

module.exports = jokeRouter;
